/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package stis.statpro.ibs.view;

import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import stis.statpro.ibs.controller.CardLayoutController;
import stis.statpro.ibs.dao.PerusahaanDao;
import stis.statpro.ibs.model.Perusahaan;

/**
 *
 * @author habib <imamhabibpamungkas@gmail.com>
 */
public class PanelRead extends javax.swing.JPanel {

    private DefaultTableModel tableModel;
    private CardLayoutController layoutController;

    /**
     * Creates new form PanelRead
     */
    public PanelRead() {
        initComponents();
        layoutController = new CardLayoutController();
        layoutController.setCardLayout((CardLayout) this.getLayout());
        layoutController.setJpanel(this);
        tableModel = (DefaultTableModel) tableData.getModel();
        comboOpsi.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                if (comboOpsi.getSelectedIndex() >= 0) {
                    setEnabled(true);
                    labelPencarian.setText("Masukkan " + e.getItem().toString());
                    labelPencarian.setVisible(true);
                    buttonCari.setVisible(true);
                    textCari.setVisible(true);
                }
            }
        });
        panelDetail.getButtonKembali().addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                layoutController.show("card3");
            }
        });
    }

    public JButton getjbuttonDetail() {
        return buttonDetail;
    }

    public JButton getButtonHome() {
        return buttonHome;
    }

    public JButton getjButton3() {
        return buttonCari;
    }

    public JComboBox getjComboBox1() {
        return comboOpsi;
    }

    public JScrollPane getjScrollPane3() {
        return jScrollPane3;
    }

    public JTable getjTable2() {
        return tableData;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        tableData = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        comboOpsi = new javax.swing.JComboBox();
        labelPencarian = new javax.swing.JLabel();
        textCari = new javax.swing.JTextField();
        buttonCari = new javax.swing.JButton();
        buttonDetail = new javax.swing.JButton();
        buttonHome = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        panelDetail = new stis.statpro.ibs.view.PanelDetail();

        setMaximumSize(new java.awt.Dimension(1000, 850));
        setMinimumSize(new java.awt.Dimension(547, 353));
        setPreferredSize(new java.awt.Dimension(1000, 850));
        setLayout(new java.awt.CardLayout());

        jPanel1.setLayout(new java.awt.BorderLayout());

        tableData.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "KIP", "Propinsi", "Kabupaten", "Kecamatan", "Nama Perusahaan", "TanggalUpdate"
            }
        ));
        tableData.getTableHeader().setReorderingAllowed(false);
        jScrollPane3.setViewportView(tableData);

        jPanel1.add(jScrollPane3, java.awt.BorderLayout.CENTER);

        jPanel2.setPreferredSize(new java.awt.Dimension(547, 103));

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel1.setText("Pencarian berdasarkan :");

        comboOpsi.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Kip", "Propinsi", "Kabupaten", "Nama Perusahaan" }));

        labelPencarian.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        labelPencarian.setText("Masukkan KIP :");

        buttonCari.setText("Cari");
        buttonCari.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCariActionPerformed(evt);
            }
        });

        buttonDetail.setText("Detail");
        buttonDetail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonDetailActionPerformed(evt);
            }
        });

        buttonHome.setText("Home");
        buttonHome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonHomeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(104, 104, 104)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(labelPencarian, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(comboOpsi, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(textCari, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonCari)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(buttonDetail)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonHome)
                        .addGap(6, 6, 6))))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(comboOpsi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelPencarian)
                    .addComponent(textCari, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonCari)
                    .addComponent(buttonDetail, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonHome))
                .addContainerGap(41, Short.MAX_VALUE))
        );

        jPanel1.add(jPanel2, java.awt.BorderLayout.PAGE_START);

        add(jPanel1, "card3");

        jScrollPane1.setViewportView(panelDetail);

        add(jScrollPane1, "card2");
    }// </editor-fold>//GEN-END:initComponents

    private void buttonCariActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCariActionPerformed
        isiTable();
        textCari.setText("");
    }//GEN-LAST:event_buttonCariActionPerformed

    private void buttonDetailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonDetailActionPerformed
        isiDetail();
        layoutController.show("card2");
    }//GEN-LAST:event_buttonDetailActionPerformed

    private void buttonHomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonHomeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_buttonHomeActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonCari;
    private javax.swing.JButton buttonDetail;
    private javax.swing.JButton buttonHome;
    private javax.swing.JComboBox comboOpsi;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JLabel labelPencarian;
    private stis.statpro.ibs.view.PanelDetail panelDetail;
    private javax.swing.JTable tableData;
    private javax.swing.JTextField textCari;
    // End of variables declaration//GEN-END:variables

    private void isiTable() {
        try {
            PerusahaanDao po = new PerusahaanDao();
            List<Perusahaan> listData = null;
            Perusahaan p;
            switch (comboOpsi.getSelectedIndex()) {
                case 0:
                    listData = po.getPerusahaanByKipDelete(Integer.parseInt(textCari.getText()));
                    clearTable();
                    //tableModel.addRow(new Object[]{p.getKip(), p.getPropinsi(), p.getKabupaten(), p.getKecamatan(), p.getNamaPrsh()});
                    break;
                case 1:
                    listData = po.getPerusahaanByProp(textCari.getText());
                    clearTable();
                    break;
                case 2:
                    listData = po.getPerusahaanByKab(textCari.getText());
                    clearTable();
                    break;
                case 3:
                    listData = po.getPerusahaanByNama(textCari.getText());
                    clearTable();
                    break;
            }
            if (comboOpsi.getSelectedIndex() == 0) {
                for (Perusahaan pi : listData) {
                    tableModel.addRow(new Object[]{pi.getKip(), pi.getPropinsi(), pi.getKabupaten(), pi.getKecamatan(), pi.getNamaPrsh(), pi.getTanggalInput()});
                }
            } else {
                for (Perusahaan pi : listData) {
                    if (pi.isUpdateData()) {
                        tableModel.addRow(new Object[]{pi.getKip(), pi.getPropinsi(), pi.getKabupaten(), pi.getKecamatan(), pi.getNamaPrsh(), pi.getTanggalInput()});
                    }
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(PanelRead.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private String aset(boolean b) {
        if (b) {
            return "Ada";
        } else {
            return "Tidak Ada";
        }
    }

    private void isiDetail() {
        try {
            PerusahaanDao po = new PerusahaanDao();
            int ind = (int) tableData.getValueAt(tableData.getSelectedRow(), 0);
            Perusahaan p = po.getPerusahaanByKip(ind);
            panelDetail.getLabelAlamatPrsh().setText(p.getAlamatPrsh());
            panelDetail.getLabelAlamatPsat().setText(p.getAlamatKantorPusat());
            panelDetail.getLabelAsetBangunan().setText(aset(p.isAsetBangunan()));
            panelDetail.getLabelAsetKend().setText(aset(p.isAsetKendaraan()));
            panelDetail.getLabelAsetLain().setText(aset(p.isAsetLainnya()));
            panelDetail.getLabelAsetMesin().setText(aset(p.isAsetMesindanAlatBerat()));
            panelDetail.getLabelAsetRupiah().setText(Long.toString(p.getAsetDalamRupiah()));
            panelDetail.getLabelDesa().setText(p.getDesaKelurahan());
            panelDetail.getLabelEmailPrsh().setText(p.getEmailPrsh());
            panelDetail.getLabelEmailPsat().setText(p.getEmailKantorPusat());
            panelDetail.getLabelFaxPrsh().setText(p.getFaxPrsh());
            panelDetail.getLabelFaxPsat().setText(p.getFaxKantorPusat());
            panelDetail.getLabelGadget().setText(aset(p.isAsetGadget()));
            panelDetail.getLabelKab().setText(p.getKabupaten());
            panelDetail.getLabelKec().setText(p.getKecamatan());
            panelDetail.getLabelKip().setText(Integer.toString(p.getKip()));
            panelDetail.getLabelKodePosPrsh().setText(p.getKodeposPrsh());
            panelDetail.getLabelKodePosPsat().setText(p.getKodeposKantorPusat());
            panelDetail.getLabelKotaAdm().setText(p.getKotaAdministratif());
            panelDetail.getLabelLokasi().setText(p.getLokasiPrsh());
            panelDetail.getLabelModal().setText(p.getStatusPenanamanModal());
            panelDetail.getLabelNamaPrsh().setText(p.getNamaPrsh());
            panelDetail.getLabelOmset().setText(Long.toString(p.getOmzetsebulanDalamRupiah()));
            panelDetail.getLabelProp().setText(p.getPropinsi());
            panelDetail.getLabelTeleponPrsh().setText(p.getTelpPrsh());
            panelDetail.getLabelTeleponPsat().setText(p.getTelpKantorPusat());
            panelDetail.getLabelTenagaKerja().setText(Integer.toString(p.getTenagakerja()));
        } catch (SQLException ex) {
            Logger.getLogger(PanelRead.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void clearTable() {
        while (tableData.getRowCount() > 0) {
            tableModel.removeRow(0);
        }
    }
}
